<?php          

/**********************************************
  Autor:Rafal Sadlowski 
  e-mail:rafal.sadlowski@gmail.com 
  data: 10.09.2007
**********************************************/               

include_once('Ustawienia.inc');

class Database {
/*
      Podstawowy interfejs bazy
*/
 private $zrodlo;
 private $result;
 
 public function __construct(){
      require_once(baza_path);
 
          $this->zrodlo=mysql_connect('localhost',uzytkownik,haslo);
          mysql_select_db(baza_danych)or die('Nie mozna znalezc bazy');
 }
 
      
      public function query($query){
            $this->result=mysql_query($query);
            if(!$this->result){
               //   echo "ERR:nie wykonano zapytaniaw query:".$query;
                  return -1;
            }
      }
       
      public function close(){
        //    mysql_close($this->zrodlo);
      }
     
         
      public function resultArray(){ 
            if(is_resource($this->result)){
             $row=array();
             $i=0;
                  while ($rowOne = mysql_fetch_array($this->result, MYSQL_NUM)){$row[$i]=$rowOne;$i++;}
                  mysql_free_result($this->result);      
                  return $row;     
            }else
            return -1;
      }
        
      public function resultAssoc(){
            if(is_resource($this->result)){
              $row=array();
             		$i=0;
                  while ($rowOne = mysql_fetch_array($this->result, MYSQL_ASSOC)){$row[$i]=$rowOne;$i++;}
                  mysql_free_result($this->result);      
                  return $row;     
            }else
            return -1;
      } 
      
        public function oneValue(){ 
            if(is_resource($this->result)){
                 $row=mysql_fetch_array($this->result, MYSQL_NUM);
                  mysql_free_result($this->result);      
                  return $row[0];     
            }else
            return -1;
      }
      public function delete($tablica,$t_WHERE){
            $up="DELETE FROM`$tablica`";
           
            if(isset($t_WHERE)){
            $i=0;
            $up.=" WHERE";
                  foreach($t_WHERE as  $encja=>$rekord){
                        if($i++==0) $up.=" `$encja`='$rekord'";
                        else  $up.=" AND `$encja`='$rekord'";
              }
            } 
            return $this->query($up);
      }
      
      public function update($tablica,$t_SET,$t_WHERE){
            $up="UPDATE `$tablica` SET";
            if(isset($t_SET)){
            $i=0;
            $up.="";
                  foreach($t_SET as  $encja=>$rekord){
                        if($i++==0) $up.=" `$encja`='$rekord'";
                        else  $up.=", `$encja`='$rekord'";
              }
            }
            if(isset($t_WHERE)){
            $i=0;
            $up.=" WHERE";
                  foreach($t_WHERE as  $encja=>$rekord){
                        if($i++==0) $up.=" `$encja`='$rekord'";
                        else  $up.=" AND `$encja`='$rekord'";
              }
            }    //  echo $up;  
            return $this->query($up);
      }
      
      
      public function select($tablica,$t_VALUES,$t_WHERE){
      $up="SELECT ";
      $i=0;           if($t_VALUES[0]!='*'){
            foreach($t_VALUES as $val){
                  if($i++>0)$up.=",`$val`";
                  else $up.="`$val`";    
            }
      }else $up.="*" ;
      $up.=" FROM $tablica";
            if(isset($t_WHERE)){
            $i=0;
            $up.=" WHERE";
                  foreach($t_WHERE as  $encja=>$rekord){
                        if($i++==0) $up.=" `$encja`='$rekord'";
                        else  $up.=" AND `$encja`='$rekord'";
              }
            }
             // echo $up;
            $r=$this->query($up);
            return $r;
            
      }
      public function insert($tablica,$t_VALUES){
            $up="INSERT INTO `$tablica`(";
            $i=0;
            $up.="";
                  foreach($t_VALUES as  $encja=>$rekord){
                        if($i++==0) $up.="`$encja`";
                        else  $up.=", `$encja`";
                    }
            $up.=") VALUES (";
             $i=0;
                  foreach($t_VALUES as  $encja=>$rekord){
                        if($i++==0) $up.="'$rekord'";
                        else  $up.=", '$rekord'";
                    }
            
               $up.=')';                
            if(isset($t_VALUES)) return $this->query($up);
            else return -1;
      }
      
      
}

?>
